package streamobject

import (
	"yizhuoyan/go-study/pdfgo/handyio"
	"yizhuoyan/go-study/pdfgo/raw/objects"
	"yizhuoyan/go-study/pdfgo/raw/objects/basicobject"
)

var StartStreamKeywordBytes = []byte("stream")
var EndStreamKeywordBytes = []byte("endstream")

type StreamObject interface {
	objects.Object
	GetType() string
	GetDictionary() *basicobject.Dictionary
	ReadStream(dic *basicobject.Dictionary, r handyio.PdfRawReader) error
}

func ReadStream(dic *basicobject.Dictionary, r handyio.PdfRawReader) (StreamObject, error) {
	typeName := dic.MustGetName("/Type")
	if typeName == nil {
		var rst = new(BytesStreamObject)
		if err := rst.ReadStream(dic, r); err != nil {
			return nil, err
		}
		return rst, nil
	}
	switch typeName.GetNameValue() {
	case typeNameOfObjStm:
		var rst = new(ObjectStreamComponent)
		if err := rst.ReadStream(dic, r); err != nil {
			return nil, err
		}
		return rst, nil
	case typeNameOfXref:
		var rst = new(CrossReferenceStreamObject)
		if err := rst.ReadStream(dic, r); err != nil {
			return nil, err
		}
		return rst, nil
	}
	return nil, nil
}
